package org.quinigold.servlet;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.quinigold.model.PojoUtil;

@SuppressWarnings("serial")
public class HelloWorldJsp extends HttpServlet {
    private static final Logger log = Logger.getLogger(HelloWorldJsp.class.getName());

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
	try {
	    log.warning("HelloWorldJsp:doGet");

	    PojoUtil pojo = new PojoUtil();
	    pojo.setPropiedadInt(23);
	    pojo.setPropiedadString("propiedad String");

	    String propiedadInt = (String) req.getParameter("propiedadInt");
	    String propiedadString = (String) req.getParameter("propiedadString");

	    if (StringUtils.isNotBlank(propiedadString))
		pojo.setPropiedadString(propiedadString);
	    if (StringUtils.isNumeric(propiedadInt))
		pojo.setPropiedadInt(NumberUtils.toInt(propiedadInt, 1));

	    req.setAttribute("pojo", pojo);

	    RequestDispatcher dispatcher = req.getRequestDispatcher("WEB-INF/jsp/helloWorldJsp.jsp");
	    dispatcher.forward(req, resp);

	} catch (Exception e) {
	    log.warning(ExceptionUtils.getStackTrace(e));
	}
    }

    public void doPost(HttpServletRequest req, HttpServletResponse resp) {

	try {
	    log.warning("HelloWorldJsp:doPost");
	    PojoUtil pojo = new PojoUtil();
	    pojo.setPropiedadInt(23);
	    pojo.setPropiedadString("propiedad String");

	    String propiedadInt = (String) req.getParameter("propiedadInt");
	    String propiedadString = (String) req.getParameter("propiedadString");

	    if (StringUtils.isNotBlank(propiedadString))
		pojo.setPropiedadString(propiedadString);
	    if (StringUtils.isNumeric(propiedadInt))
		pojo.setPropiedadInt(NumberUtils.toInt(propiedadInt, 1));

	    req.setAttribute("pojo", pojo);

	    RequestDispatcher dispatcher = req.getRequestDispatcher("WEB-INF/jsp/helloWorldJsp.jsp");
	    dispatcher.forward(req, resp);
	} catch (Exception e) {
	    log.warning(ExceptionUtils.getStackTrace(e));
	}
    }
}
